package com.ruoyi.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.Employee;
import com.ruoyi.business.domain.Talentpool;
import com.ruoyi.business.domain.vo.EmployeeVo;
import com.ruoyi.business.service.IEmployeePlusService;
import com.ruoyi.business.service.ITalentpoolService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.poi.ExcelUtilPlus;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 人才库
 *
 * @author songzhengsheng
 * @date 2023-09-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/talentpool")
public class TalentpoolController extends BaseController {

    private final ITalentpoolService iTalentpoolService;
    private final IEmployeePlusService employeePlusService;
    /**
     * 查询员工列表
     */
    @GetMapping("/list")
    public TableDataInfo<Talentpool> list(Talentpool entity) {
        startPage();
        LambdaQueryWrapper<Talentpool> lqw =iTalentpoolService.buildQueryWrapper(entity);
        List<Talentpool> list = iTalentpoolService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出员工列表
     */
    @Log(title = "员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Talentpool entity, HttpServletResponse response) {
        LambdaQueryWrapper<Talentpool> lqw =iTalentpoolService.buildQueryWrapper(entity);
        List<Talentpool> list = iTalentpoolService.list(lqw);
        ExcelUtilPlus<Talentpool> util = new ExcelUtilPlus<Talentpool>(Talentpool.class);
        util.exportExcel(response, list, "人才库");
    }

    /**
     * 获取员工详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<Talentpool> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTalentpoolService.getById(id));
    }

    /**
     * 新增员工
     */
    @SaIgnore
    @Log(title = "员工", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Talentpool bo) {
        return toAjax(iTalentpoolService.save(bo));
    }

    /**
     * 修改员工
     */
    @Log(title = "员工", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Talentpool bo) {
        return toAjax(iTalentpoolService.updateById(bo));
    }

    /**
     * 删除员工
     *
     * @param ids 主键串
     */
    @Log(title = "员工", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTalentpoolService.removeBatchByIds(Arrays.asList(ids), true));
    }

    /**
     * 转正
     *
     */
    @Log(title = "转正", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping(value = "/become")
    public R<Void> become(@Validated(AddGroup.class) @RequestBody Talentpool bo) {
        Talentpool entity=iTalentpoolService.getById(bo);
        Employee employee=new Employee();
        employee.setFullName(entity.getFullName());
        employee.setDepartment(entity.getDepartment());
        employee.setPosition(entity.getPosition());
        employee.setAge(entity.getAge());
        employee.setGender(entity.getGender());
        employee.setEthnicity(entity.getEthnicity());
        employee.setIdNumber(entity.getIdNumber());
        employee.setHomeAddress(entity.getHomeAddress());
        employee.setResidentialAddress(entity.getResidentialAddress());
        employee.setContactNumber(entity.getContactNumber());
        employee.setEmergencyContactPerson(entity.getEmergencyContactPerson());
        employee.setEmergencyContactNumber(entity.getEmergencyContactNumber());
        employee.setEducation(entity.getEducation());
        employee.setMajor(entity.getMajor());
        employee.setRuralMedicalInsurance(entity.getRuralMedicalInsurance());
        employee.setAccommodation(entity.getAccommodation());
        employee.setDelFlag(entity.getDelFlag());
        employee.setSecondemployment(entity.getSecondemployment());
        employee.setImg(entity.getImg());
        employee.setRemarks(entity.getRemarks());
        employee.setOutsource(bo.getOutsource());
        employeePlusService.save(employee);
        return toAjax(true);
    }
}
